package io.file;

import framework.Testable;
import utils.ArrayUtils;
import utils.Log;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

/**
 * Created by shane on 2018/5/19.
 */
public class DirList implements Testable {

    @Override
    public void test(String[] args) {
        File file = new File("/Users/shane/workspace/javatests/app/src/test/java/com/shane/java/javatests");
        String[] list;
        if (ArrayUtils.isEmpty(args)) {
            list = file.list();
        } else {
            list = file.list(new DirFilter(args[0]));
        }

        Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
        for (String name : list) {
            Log.v(name);
        }
    }

    static class DirFilter implements FilenameFilter {
        private Pattern mPattern;

        DirFilter(String regex) {
            mPattern = Pattern.compile(regex);
        }

        @Override
        public boolean accept(File dir, String name) {
            return mPattern.matcher(name).matches();
        }
    }
}
